package cn.iocoder.yudao.module.promotion.convert.rankinglist;

import cn.hutool.core.util.DesensitizedUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.rankinglist.vo.RankingListUserInfoRespVo;
import cn.iocoder.yudao.module.promotion.controller.app.rankinglist.vo.AppRankingListDetailRespVo;
import cn.iocoder.yudao.module.promotion.controller.app.rankinglist.vo.AppRankingListUserInfoRespVo;
import cn.iocoder.yudao.module.promotion.dal.dataobject.rankinglist.RankingListDO;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageRecordDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

/**
 * 排行榜 Convert
 *
 * @author HUIHUI
 */
@Mapper
public interface RankingListConvert {

    RankingListConvert INSTANCE = Mappers.getMapper(RankingListConvert.class);

    AppRankingListDetailRespVo convert(RankingListDO bean);

    default AppRankingListUserInfoRespVo convert(BrokerageRecordDto brokerageRecordDto){
        return new AppRankingListUserInfoRespVo().setUserId(brokerageRecordDto.getUserId()).setCount(brokerageRecordDto.getCount()).setScore(brokerageRecordDto.getScore());
    }

    default AppRankingListUserInfoRespVo convert(BrokerageRecordDto brokerageRecordDto,MemberUserRespDTO memberUserRespDTO){
        AppRankingListUserInfoRespVo appRankingListUserInfoRespVo = convert(brokerageRecordDto);
        appRankingListUserInfoRespVo.setMobile(DesensitizedUtil.mobilePhone(memberUserRespDTO.getMobile()));
        return appRankingListUserInfoRespVo;
    }

    default AppRankingListDetailRespVo convert(RankingListDO rankingListDO, List<BrokerageRecordDto> brokerageRecordList,List<MemberUserRespDTO> memberUserList){
        AppRankingListDetailRespVo appRankingListDetailRespVo = convert(rankingListDO);
        Map<Long,String> userMap = CollectionUtils.convertMap(memberUserList,MemberUserRespDTO::getId,MemberUserRespDTO::getMobile);
        List<AppRankingListUserInfoRespVo> list = CollectionUtils.convertList(brokerageRecordList, brokerageRecordDto -> {
            AppRankingListUserInfoRespVo appRankingListUserInfoRespVo = convert(brokerageRecordDto);
            appRankingListUserInfoRespVo.setMobile(DesensitizedUtil.mobilePhone(userMap.get(brokerageRecordDto.getUserId())));
            return appRankingListUserInfoRespVo;        });
        appRankingListDetailRespVo.setUserList(list);
        return appRankingListDetailRespVo;
    }

    default List<RankingListUserInfoRespVo> convert(List<BrokerageRecordDto> brokerageRecordList,List<MemberUserRespDTO> memberUserList){
        Map<Long,MemberUserRespDTO> userMap = CollectionUtils.convertMap(memberUserList,MemberUserRespDTO::getId,user->user);
        List<RankingListUserInfoRespVo> list = CollectionUtils.convertList(brokerageRecordList, brokerageRecordDto -> {
            RankingListUserInfoRespVo rankingListUserInfoRespVo = new RankingListUserInfoRespVo();
            MemberUserRespDTO memberUser = userMap.get(brokerageRecordDto.getUserId());
            rankingListUserInfoRespVo.setUserId(brokerageRecordDto.getUserId()).setCount(brokerageRecordDto.getCount()).setScore(brokerageRecordDto.getScore()).setPrice(brokerageRecordDto.getPrice());
            rankingListUserInfoRespVo.setNickname(memberUser.getNickname());
            rankingListUserInfoRespVo.setMobile(DesensitizedUtil.mobilePhone(memberUser.getMobile()));
            return rankingListUserInfoRespVo;        });
        return list;
    }



}
